/**
 * Copyright 2009 Erick Armbrust 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0 
 *   
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */

package tapioca.dt;

import java.util.List;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Transaction;
import com.google.appengine.repackaged.com.google.common.collect.Lists;

import tapioca.testing.AppEngineTestCase;

/**
 * Tests for the {@link DistributedTransactionEntity} class.
 *
 * @author armbrust@gmail.com (Erick Armbrust)
 */
public class DistributedTransactionEntityTest extends AppEngineTestCase {

  public void testCreate() {
    DistributedTransactionEntity dte = new DistributedTransactionEntity();
    assertEquals(State.INIT_0, dte.getState());
    assertTrue(dte.getGets().isEmpty());
    assertTrue(dte.getPuts().isEmpty());
  }
  
  public void testBasicLoadFromEntity() throws Exception {
    DistributedTransactionEntity dte = new DistributedTransactionEntity();
    Transaction txn = ds.beginTransaction();
    dte.save(ds, txn);
    txn.commit();

    DistributedTransactionEntity dteCopy = new DistributedTransactionEntity(
        ds.get(dte.getEntity().getKey()));
    assertEquals(State.INIT_0, dteCopy.getState());
    assertTrue(dteCopy.getGets().isEmpty());
    assertTrue(dteCopy.getPuts().isEmpty());
  }
  
  public void testPopulateSaveAndLoad() throws Exception {
    Key key1 = KeyFactory.createKey("foo", 1L);
    Key key2 = KeyFactory.createKey("foo", 2L);
    Key shadowKey1 = KeyFactory.createKey("shadow", 1L);
    Key shadowKey2 = KeyFactory.createKey("shadow", 2L);
    GetRequest get1 = new GetRequest(key1, dummyVersion);
    GetRequest get2 = new GetRequest(key1, dummyVersion);
    PutRequest put1 = new PutRequest(key1, shadowKey1, false);
    PutRequest put2 = new PutRequest(key2, shadowKey2, true);
    
    DistributedTransactionEntity dte = new DistributedTransactionEntity();
    dte.setState(State.READY_1);
    dte.addGet(get1);
    dte.addPut(put1);
    dte.addGet(get2);
    dte.addPut(put2);
    dte.save(ds);
    
    DistributedTransactionEntity dteCopy = new DistributedTransactionEntity(
        ds.get(dte.getEntity().getKey()));
    assertEquals(State.READY_1, dteCopy.getState());
    assertEquals(Lists.newArrayList(get1, get2), dteCopy.getGets());
    assertEquals(Lists.newArrayList(put1, put2), dteCopy.getPuts());
  }
  
  public void testPopulateFromNonDtEntity() throws Exception {
    Entity notDtEntity = createAndSaveDtEntity("foo", "bar");
    
    try {
      new DistributedTransactionEntity(notDtEntity);
      fail("Should have failed on loading invalid entity");
    } catch (IllegalArgumentException e) { /* Good */ }
  }
  
  @SuppressWarnings("unchecked")
  public void testLoadMangledDtEntity() throws Exception {
    Key key1 = KeyFactory.createKey("foo", 1L);
    Key key2 = KeyFactory.createKey("foo", 2L);
    Key shadowKey1 = KeyFactory.createKey("shadow", 1L);
    Key shadowKey2 = KeyFactory.createKey("shadow", 2L);
    GetRequest get1 = new GetRequest(key1, dummyVersion);
    GetRequest get2 = new GetRequest(key1, dummyVersion);
    PutRequest put1 = new PutRequest(key1, shadowKey1, false);
    PutRequest put2 = new PutRequest(key2, shadowKey2, true);
    
    DistributedTransactionEntity dte = new DistributedTransactionEntity();
    dte.setState(State.READY_1);
    dte.addGet(get1);
    dte.addPut(put1);
    dte.addGet(get2);
    dte.addPut(put2);
    dte.fillEntity();
    
    // Purposefully mangle put array and then save.
    List<Key> putShadowKeys = (List<Key>) dte.getEntity().getProperty(
        DistributedTransactionEntity.PUT_SHADOW_KEYS_PROP);
    putShadowKeys.remove(1);
    dte.getEntity().setProperty(DistributedTransactionEntity.PUT_SHADOW_KEYS_PROP, putShadowKeys);
    ds.put(dte.getEntity());
    
    try {
      new DistributedTransactionEntity(ds.get(dte.getEntity().getKey()));
      fail("DT entity is mangled and should fail load");
    } catch (RuntimeException e) { /* Good */ }
    
    // Purposefully mangle get array and then save.
    List<Key> getVersions = (List<Key>) dte.getEntity().getProperty(
        DistributedTransactionEntity.GET_VERSIONS_PROP);
    getVersions.remove(1);
    dte.getEntity().setProperty(DistributedTransactionEntity.GET_VERSIONS_PROP, getVersions);
    ds.put(dte.getEntity());    
    
    try {
      new DistributedTransactionEntity(ds.get(dte.getEntity().getKey()));
      fail("DT entity is mangled and should fail load");
    } catch (RuntimeException e) { /* Good */ }    
  }
  
}
